import { loginApi, logout, getInfoApi, getUserDetailById } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { constantRoutes, resetRouter } from '@/router'
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    userInfo: {},
    route: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
    // 存储到cookie中  持久化
    setToken(token)
  },
  SET_INFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  setRoute(state, route) {
    state.route = [...constantRoutes, ...route]
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    return new Promise(async(resolve, reject) => {
      // loginApi(userInfo)
      //   .then((response) => {
      //     console.log(response, 678)
      //     // 存储在vuex中
      //     commit('SET_TOKEN', response.data)
      //     // setToken(data.token)
      //     // 存储完token之后 需要跳转至首页
      //     resolve()
      //   })
      //   .catch((error) => {
      //     reject(error)
      //   })

      const response = await loginApi(userInfo).catch((error) => {
        reject(error)
      })

      if (response.code === 10000) {
        console.log(response, 555)
        //  存储在vuex中
        commit('SET_TOKEN', response.data)
        //  存储完token之后 需要跳转至首页
        resolve()
      }
    })
  },

  // get user info

  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfoApi()
        .then(async(response) => {
          //  返回的是用户信息
          console.log(response, 7777)

          //  2-  将获取的用户信息存储到vuex中
          // const { data } = response
          if (response.code === 10000) {
            // 获取用户图像
            const res = await getUserDetailById(response.data.userId)
            console.log(res, 66)
            commit('SET_INFO', { ...response.data, ...res.data })
            //  将获取的用户的图片存储仓库中
            commit('SET_AVATAR', res.data.staffPhoto)
            resolve(response)
          }

          // if (!data) {
          //   return reject('Verification failed, please Login again.')
          // }

          // const { name, avatar } = data

          // commit('SET_NAME', name)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // logout(state.token)
      //   .then(() => {
      //     removeToken() // must remove  token  first
      //     resetRouter()
      //     commit('RESET_STATE')
      //     resolve()
      //   })
      //   .catch((error) => {
      //     reject(error)
      //   })

      removeToken() // must remove  token  first
      resetRouter()
      commit('RESET_STATE')
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
